﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Web.UI.DataVisualization.Charting;


namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class testreport : PageBase
    {
       
        protected override string DWZRef
        {
            get
            {
                return "ODailyReport";
            }
        }
        #region 属性 (查询条件)
        protected int screeWidth
        {
            get
            {
                var x= GetRequestValue<int>("screeWidth", 800);
                return x - 80;
            }
        }

        protected string WorkDateForm
        {
            get
            {
                return GetRequestValue<string>("WorkDateForm", DateTime.Now.ToString("yyyy-MM-01"));

            }
        }

        protected string WorkDateTo
        {
            get
            {
                return GetRequestValue<string>("WorkDateTo", DateTime.Now.ToString("yyyy-MM-dd"));
            }
        }

        protected string ReportType
        {
            get
            {
                return GetRequestValue<string>("ReportType", "1");
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = 0;
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            Response.AddHeader("pragma", "no-cache");
            Response.CacheControl = "no-cache";


            switch (ReportType)
            {
                case "1":
                    DataBind1();
                    break;
                case "2":
                    DataBind2();
                    break;
                case "3":
                    DataBind3();
                    break;
                case "4":
                    DataBind4();
                    break;
                case "5":
                    DataBind5();
                    break;
                case "6":
                    DataBind6();
                    break;
            }
            DataBind();
        }

        protected void DataBind1()
        {
            DataTableCollection dts = ODailyController.GetMachineTaskTimeChart(WorkDateForm, WorkDateTo);
            rpMachineTaskTimeChart.DataSource = dts[0];
            rpMachineTaskTimeChart.DataBind();

            MachineTaskTimeChart.DataSource = dts[0];
            MachineTaskTimeChart.Series["Series1"].XValueMember = "SerialNumberNum";
            MachineTaskTimeChart.Series["Series1"].YValueMembers = "TaskTime";
            MachineTaskTimeChart.DataBind();
            MachineTaskTimeChart.Width = screeWidth;
            double tasktimes = 0;
            foreach (DataRow dr in dts[0].Rows)
            {
                tasktimes = tasktimes + Convert.ToDouble(dr["mactasktime"]);
            }

            MachineTaskTimeChart.Titles["TaskTimeTitle"].Text = string.Format("从{0}到{1}总直接工时为{2}小时", WorkDateForm, WorkDateTo, tasktimes);
            // this.rpMenu.DataSource = ODailyController.GetPagination(UserName, SerialNumber, pageNum, numPerPage, orderField, ref TotalCount);
            // this.rpMenu.DataBind();
            MachineTaskTimeChart.Series["Series1"]["DrawingStyle"] = "Cylinder";
            MachineTaskTimeChart.Series["Series1"]["PointWidth"] = "0.4";


            rpMachineTaskTimeChart2.DataSource = dts[1];
            rpMachineTaskTimeChart2.DataBind();
        }

        protected void DataBind2()
        {
            

            chart2.Series.Clear();
            
            DataTableCollection dts = ODailyController.GetMachineTaskTime2(WorkDateForm, WorkDateTo);

            foreach (DataColumn dc in dts[0].Columns)
            {
                if (dc.ColumnName == "dategroup")
                    continue;
                Series ser = new Series(dc.ColumnName) { ChartType = SeriesChartType.StackedColumn100, IsVisibleInLegend = true, IsValueShownAsLabel = true, LabelFormat = "{f1}%" };
                ser.XValueMember = "dategroup";
                ser.YValueMembers = dc.ColumnName;
                chart2.Series.Add(ser);
            }

            chart2.DataSource = dts[0];
            chart2.DataBind();
            chart2.Width = screeWidth;


            this.rp2_1.DataSource = dts[0];
            this.rp2_1.DataBind();
            //
            
            foreach (DataRow dr in dts[1].Rows)
            {
                totaltasktime += Convert.ToDecimal(dr["TaskTime"]);
            }
            this.rp2_2.DataSource = dts[1];
            this.rp2_2.DataBind();

        }
        protected decimal totaltasktime = 0m;
        protected object GetPer2(object 被除数, object 除数)
        {
            decimal x被除数 = Convert.ToDecimal(被除数);
            decimal x除数 = Convert.ToDecimal(除数);
            if (x除数 == 0)
                return 0;
            return ((x被除数 /( x除数 + x被除数)) * 100).ToString("f1") + "%";
        }
        protected object GetPer2_1(object 被除数)
        {
            decimal x被除数 = Convert.ToDecimal(被除数);
            if (totaltasktime == 0)
                return 0;
            return ((x被除数 / totaltasktime) * 100).ToString("f1") + "%";
        }

        protected void DataBind3()
        {
            DataTable dt = ODailyController.GetMachineTaskTime3(WorkDateForm, WorkDateTo);
            rpMachineTaskTimeChart3.DataSource = dt;
            rpMachineTaskTimeChart3.DataBind();
            MachineTaskTimeChart3.DataSource = dt;
            MachineTaskTimeChart3.Series["Series1"].XValueMember = "SerialNumberNum";
            MachineTaskTimeChart3.Series["Series1"].YValueMembers = "alltasktime";
            MachineTaskTimeChart3.DataBind();

            MachineTaskTimeChart3.Series["Series1"]["DrawingStyle"] = "Cylinder";
            MachineTaskTimeChart3.Series["Series1"]["PointWidth"] = "0.4";

            double tasktimes = 0;
            foreach (DataRow dr in dt.Rows)
            {
                tasktimes = tasktimes + Convert.ToDouble(dr["mactasktime"]);
            }

            MachineTaskTimeChart3.Titles["TaskTimeTitle"].Text = string.Format(MachineTaskTimeChart3.Titles["TaskTimeTitle"].Text, WorkDateForm, WorkDateTo, tasktimes);
            MachineTaskTimeChart3.Width = screeWidth;
        }
        protected void DataBind4()
        {
            DataTable dt = ODailyController.GetMachineTaskTime4(WorkDateForm, WorkDateTo);
            rpMachineTaskTimeChart4.DataSource = dt;
            rpMachineTaskTimeChart4.DataBind();
            MachineTaskTimeChart4.DataSource = dt;
            MachineTaskTimeChart4.Series["Series1"].XValueMember = "GongWei";
            MachineTaskTimeChart4.Series["Series1"].YValueMembers = "TaskTime";
            MachineTaskTimeChart4.DataBind();
            MachineTaskTimeChart4.Width = 600;
            //double tasktimes = 0;
            //foreach (DataRow dr in dt.Rows)
            //{
            //    tasktimes = tasktimes + Convert.ToDouble(dr["TaskTime"]);
            //}

            //MachineTaskTimeChart.Titles["TaskTimeTitle"].Text = string.Format("从{0}到{1}总直接工时为{2}分钟", WorkDateForm, WorkDateTo, tasktimes);
        }
        protected void DataBind5()
        {
            Chart5.Series.Clear();
            DataTable dt = ODailyController.GetMachineTaskTime5(WorkDateForm, WorkDateTo);

            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.ColumnName == "SerialNumber")
                    continue;
                Series ser = new Series(dc.ColumnName) { ChartType = SeriesChartType.StackedBar100, BorderWidth = 3, MarkerStyle = MarkerStyle.Square, IsVisibleInLegend = true, IsValueShownAsLabel = true, LabelFormat = "{f1}%" };
                ser.XValueMember = "SerialNumber";
                ser.YValueMembers = dc.ColumnName;
                ser.IsValueShownAsLabel = true;
                ser["LabelStyle"] = "Center";
                Chart5.Series.Add(ser);
            }
            //Series ser1 = new Series("80NS002") { ChartType = SeriesChartType.Line, BorderWidth = 3, MarkerStyle = MarkerStyle.Square, IsVisibleInLegend = true, IsValueShownAsLabel = true };
            //ser1.XValueMember = "JobTitle";
            //ser1.YValueMembers = "80NS002";
            //Chart1.Series.Add(ser1);

            Chart5.DataSource = dt;
            Chart5.DataBind();
            Chart5.Width = screeWidth;

            DataTable dt2 = dt.Copy();
            dt2.Columns[0].ColumnName = "项目";
            GridView5.DataSource = dt2;
            GridView5.DataBind();

            //GridView5.Columns[0].HeaderText = "项目";
        }
        protected void DataBind6()
        {
            Chart6.Series.Clear();
            DataTable dt = ODailyController.GetMachineTaskTime6(WorkDateForm, WorkDateTo);

            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.ColumnName == "jobtitle")
                    continue;

                Series ser = new Series(dc.ColumnName) { ChartType = SeriesChartType.StackedBar100, BorderWidth = 3, MarkerStyle = MarkerStyle.Square, IsVisibleInLegend = true, IsValueShownAsLabel = true, LabelFormat = "{f1}%" };
                ser.XValueMember = "jobtitle";
                ser.YValueMembers = dc.ColumnName;
                ser.IsValueShownAsLabel = true;
                ser["LabelStyle"] = "Center";
                Chart6.Series.Add(ser);
               
            }
            //Series ser1 = new Series("80NS002") { ChartType = SeriesChartType.Line, BorderWidth = 3, MarkerStyle = MarkerStyle.Square, IsVisibleInLegend = true, IsValueShownAsLabel = true };
            //ser1.XValueMember = "JobTitle";
            //ser1.YValueMembers = "80NS002";
            //Chart1.Series.Add(ser1);

            Chart6.DataSource = dt;
            Chart6.DataBind();
            Chart6.Width = screeWidth;

            DataTable dt2 = dt.Copy();
            dt2.Columns[0].ColumnName = "工作内容";
            GridView6.DataSource = dt2;
            GridView6.DataBind();
        }

       
    }
}